package engine.scene.player
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.flag.GameDefine;
	import engine.flag.ObjType;
	import engine.flag.PlayerActionFlag;
	import engine.logic.walk.Astar.ForbiddenAreaLogic;
	import engine.logic.walk.NormalMoveLogic;
	import engine.logic.walk.WalkManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	
	import newx.BaseObject;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	
	public class NpcModel extends BasePlayerModel
	{
		private var _iconPlace	: 	Sprite;
		
		private var _icon		: 	MovieClip;
		
		private var _facePoPoPlace	:   Sprite;
		/**遇到玩家表情***/
		private var _facePoPo		:MovieClip;
		private var _moveRect	: 	Rectangle;
		/***是否已经显示过表情**/
		private var _isFace		:Boolean = false;
		
		private var _run		: 	Boolean;
		
		private var _targetPoint : Point;
		
		
		private var mpLine		:	MovieClip;
		private var mpWidth	:	int;
		
		private var _initPoint	:	Point;
		
		private var _autoMove	:	Boolean;
		
		public static var moveSpeed 	:	int = 3;
		
		public static var chaseSpeed 	:	int = 20;
		
		private var _npcObj			:SceneObject;
		
		private var _effectSpace		:Sprite;
		
		public function NpcModel()
		{
			super();
			
			_iconPlace = new Sprite();
			_facePoPoPlace = new Sprite();
			_moveLogic = new NormalMoveLogic();
			
			addChild( _iconPlace );
			addChild(_facePoPoPlace);
			mpLine = ResManager.newResClass("hong血条") as MovieClip;
			mpLine.stop();
			mpWidth = mpLine.width;
			_effectSpace	=	new Sprite();
			addChild(_effectSpace);
		}
		
		public function get effectSpace():Sprite
		{
			return _effectSpace;
		}
		
		public function get iconPlace():Sprite
		{
			return _iconPlace;
		}
		
		public function get npcObj():SceneObject
		{
			return _npcObj;
		}
		
		public function get moveRect():Rectangle
		{
			return _moveRect ;
		}
		
		public function get autoMove():Boolean
		{
			return _autoMove;
		}
		
		public function get run():Boolean
		{
			return _run;
		}
		
		/**
		 *	获取玩家站位点坐标
		 * @param playerObj
		 * @return point
		 * 
		 */
		override protected function getPosPoint( playerObj : SceneObject ):Point
		{ 
			_initPoint = new Point( playerObj.GetPropertiesEx( "X" ), playerObj.GetPropertiesEx( "Y" ) );
			
			initActiveArea();
			
			return _initPoint;
		}
		
		/**
		 *获得配置的坐标 
		 * @return 
		 * 
		 */
		public function getGuidePoint():Point
		{
			return new Point( playerObj.GetPropertiesEx( "X" ), playerObj.GetPropertiesEx( "Y" ) );
		}
		//回到初始地点
		public function resetPoint():void
		{
			if( !_autoMove ) return;
			
			killMove();
			
			x = _initPoint.x;
			y = _initPoint.y;
			
			_run = true;
			
			_autoMove = false;
			
			_moveLogic.speed = moveSpeed;
			
			boosMove();
		}
		
		public function updataPlayer(playerObj:SceneObject):void
		{
			updataPlayerInfo(playerObj);
		}
		
		override protected function updataPlayerInfo( playerObj : SceneObject ):void
		{
			_npcObj = playerObj;
			//			direction = 1;
			
			super.updataPlayerInfo( playerObj );
			
			if(playerObj == null) return;
			
			
			var npcStatus : Array = getPlayerByRecord("NpcStatusRec", playerObj.ObjKey );
			
			var isStatue : Boolean;
			
			if( null != npcStatus )
			{
				isStatue = setStatusIcon( npcStatus[ 1 ] );
			}
			if( !isStatue )
			{
				setType( playerObj.Type );
			}
			
			_moveLogic.speed = moveSpeed;
			
			mpLine.visible = false;
			
			if( playerObj.Type == ObjType.NPC )
			{
				var bossStyle:int = checkBoss();
				switch(bossStyle)
				{
					case 2:
						
						mpLine.visible = true;
						addChild( mpLine );
						if(_playerMovie.overhead)
						{
							mpLine.x = _playerMovie.overhead.x  - _nameLabel.width * .5;
							mpLine.y = _nameLabel.y - 10;
						}
						else
						{
							mpLine.x = -38;
							mpLine.y = -182;
						}
						updataBoosHp( playerObj.GetPropertiesEx( "HP"), playerObj.GetPropertiesEx( "MaxHP" ));
						break;
					case 3:
						mpLine.visible = false;
						break;
					case 4:
						mpLine.visible = false;
						break;
					default:
						if(GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)
						{
							_moveRect	=	ForbiddenAreaLogic.getInstance().getNPCWalkArea(this);
						}
						else
						{
							_moveRect = new Rectangle( x, y, 300, 200 );
						}
						
				}
				_run = true;
				
				//				var ShowLine:int = playerObj.GetPropertiesEx("CommonHP");
				//				if(ShowLine>0)
				//				{
				//					mpLine.visible = true;
				//					addChild( mpLine );
				//					mpLine.x = ( _playerMovie.overhead != null ? _playerMovie.overhead.x : - 0 ) - _nameLabel.width * .5;
				//					mpLine.y = _nameLabel.y - 10;
				//					
				//					updataBoosHp( playerObj.GetPropertiesEx( "HP"), playerObj.GetPropertiesEx( "MaxHP" ));
				//				}
				//				setTimeout(function():void{boosMove()},1000);
				boosMove();
			}
		}
		
		public function updataBoosHp( hp : int, maxHp : int ):void
		{
			mpLine.getChildByName("hp_0").width = hp / maxHp * mpWidth;
		}
		
		private function getPlayerByRecord( recordName : String, objId : String ):Array
		{
			var npcs 	: Array	= 	ModelLocator.getInstance().scene.GetRecordEx(recordName).recArr;
			
			for each(var npc : Array in npcs)
			{
				if((npc[0].x + "." + npc[0].y) == objId )
				{
					return npc;
				}
			}
			return null;
		}
		
		private function initActiveArea():void
		{
			var boosMoveRect : Object = StaticIni.getIniObj( "boos_moveRect" );
			if(GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)
			{
				_moveRect	=	ForbiddenAreaLogic.getInstance().getNPCWalkArea(this);
				return;
			}
			_moveRect = new Rectangle( boosMoveRect.x, boosMoveRect.y, boosMoveRect.w, boosMoveRect.h );
			WalkManager.getInstance().findWayLogic.updaeNPCWalkPlace([_moveRect.x,_moveRect.y,_moveRect.width,_moveRect.height]);
		}
		
		public function setType( type : int ):void
		{
			var frame : int = 0;
			if( type == ObjType.TradeNPC )
			{
				var tradeNO : int = playerObj.GetPropertiesEx( "TradeNo" );
				
				//					1.装备商人
				//					2.道具商人
				//					3.秘宝商人
				//					4.仓库商人
				//					5.客栈商人
				//					10.装备商人
				switch( tradeNO )
				{
					case 1:
					case 10:
						frame = 2;
						break;
					case 2:
						frame = 4;
						break;
					case 3:
					case 30:
					case 31:
					case 32:
					case 33:
					case 34:
						frame = 1;
						break;
					case 36:
					case 37:
						frame = 1;
						break;
					case 35:
						frame = 12;
				}
			}else if( type == ObjType.HotelNpc )
			{
				frame = 6;
			}else if( type == ObjType.DepotNPC )
			{
				frame = 3;
			}else if( type == ObjType.GiftNpc )
			{
				frame = 9;
			}
			
			showIcon( frame );
		}
		
		public function setStatusIcon( status : int ):Boolean
		{
			var frame : int = 0;
			switch (status) 
			{
				case GameDefine.NPCStatus_CanSubmit:			// 可提交
					frame = 7;
					break;
				case GameDefine.NPCStatus_UnComplete:			// 未完成
					frame = 8;
					break;
				case GameDefine.NPCStatus_TaskAccept:			// 可接取任务
					frame = 5;
					break;
				case GameDefine.NPCStatus_Adventure :			// 奇遇图标
					frame =11;
					break ;
			}
			showIcon( frame );
			
			return frame != 0;
		}
		
		private function showIcon( frame : int ):void
		{
			if( _playerMovie == null )return;
			if( 0 != frame )
			{
				if( null == _icon )
				{
					_icon = ResManager.newResClass( "custSkin_0021" ) as MovieClip;
					_iconPlace.addChild( _icon );
				}
				
				_icon.gotoAndStop( frame );
				
				_iconPlace.y = _nameLabel.y - _icon.height;
				_iconPlace.x = ( _playerMovie.overhead != null ? _playerMovie.overhead.x : 100 ) - _iconPlace.width * .5;
				_iconPlace.visible = _iconPlace.mouseChildren = _iconPlace.mouseEnabled = true;
			}else
			{
				_iconPlace.visible = _iconPlace.mouseChildren = _iconPlace.mouseEnabled = false;
			}
		}
		
		override public function set status(value:String):void
		{
			if( playerObj == null ) return;
			
			if( ObjType.NPC == playerObj.Type && value == PlayerActionFlag.NormalWait ) 
			{
				super.status = PlayerActionFlag.Wait;
			}else
			{
				super.status = value;
			}
		}
		
		/**
		 *NPC移动 
		 * 
		 */
		public function boosMove():void
		{
			if( !_run ) return;
			if(GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene && _moveRect==null) return;
			if(GameData.SceneType==GameDefine.SceneType_Normal) return;
			var targePoint : Point = new Point();
			targePoint.x = _moveRect.x + int( _moveRect.width * Math.random() );
			targePoint.y = _moveRect.y + int( _moveRect.height * Math.random() );
			startMove( [ targePoint ], wait );
		}
		
		/**
		 *	NPC开始移动  
		 */
		override public function startMove(path:Array, callBackFunc:Function=null, pList:*=null):void
		{
			if(GameData.SceneType!=GameDefine.SceneType_ForbiddenAreaScene && GameData.SceneType!=GameDefine.SceneType_SnowBallFightScene)
			{
				_moveLogic.startMove( this, point, [ WalkManager.getInstance().findWayLogic.getNPCWalkNode( path[ 0 ] ) ], callBackFunc, pList );
			}
			else
			{
				_moveLogic.startMove( this, point, [path[0]], callBackFunc, pList );
			}
		}
		
		private function wait():void
		{
			setTimeout( boosMove, 2000 );
		}
		
		public function npcautoMove( targePoint : Point ):void
		{
			_autoMove = true;
			
			_moveLogic.speed = chaseSpeed;
			
			_targetPoint = targePoint;
			
			super.startMove( [ _targetPoint ], chaseMove );
		}
		
		public function removeFacePoPo():void
		{
			if(_facePoPo!=null)
			{
				
				_facePoPoPlace.removeChild(_facePoPo);
				_facePoPo = null;
			}
			_facePoPoPlace.visible = false;
		}
		
		/**
		 *添加表情泡泡 
		 * 
		 */
		public  function addFacePoPo():void
		{
			if(_facePoPo == null)
			{
				_facePoPo = ResManager.newResClass( "CustomControl_059" ) as MovieClip;
			}
			if(_facePoPoPlace.numChildren <= 0)
			{
				_facePoPoPlace.y = _nameLabel.y - 10;//_facePoPo.height+40;
				_facePoPoPlace.x = ( _playerMovie.overhead != null ? _playerMovie.overhead.x -80: - 130 ) - _facePoPoPlace.width ;
				_facePoPoPlace.addChild(_facePoPo);
			}else
			{
				_facePoPoPlace.visible = true;
			}
		}
		
		/**
		 *	追击
		 */		
		private function chaseMove():void
		{
			if( _run )
			{
				_run = false;
				
				super.startMove( [ _targetPoint ], chaseMove );
			}	
		}
		
		override public function reset():void
		{
			removeFacePoPo();
			_run = false;
			
			killMove();
			
			_iconPlace.visible = _iconPlace.mouseChildren = _iconPlace.mouseEnabled = false;
			
			super.reset();
			
			removeFacePoPo();
		}
		
		public function checkBoss():int
		{
			var style:int = npcObj.GetPropertiesEx("CreatorType");//2小；3大
			return style;
		}
		
		override public function get point():Point
		{
			if( playerObj != null && playerObj.Type != ObjType.NPC )
			{
				return getGuidePoint();
			}
			
			return super.point;
		}
	}
}